package org.idtem.component.wordnet.connection;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;


public class WordNetConnection {

	private static WordNetConnection _wnconnection = null;

	private Connection _jdbcConnection = null; 
	
	private java.sql.PreparedStatement _getVariantsStatment = null;
	private java.sql.PreparedStatement _getSynsetStatment = null;
	private java.sql.PreparedStatement _getDirectRelationStatment = null;
	private java.sql.PreparedStatement _getInverseRelationStatment = null;
	private java.sql.PreparedStatement _getVariantsTogetOffsets = null;
	
	private WordNetConnection()
	{
		String userName = "root";
        String password = "root";
        String url = "jdbc:mysql://localhost:3306/spanishwn";
        try {
			Class.forName("com.mysql.jdbc.Driver").newInstance ();
	        _jdbcConnection = DriverManager.getConnection(url, userName, password);
	        _getVariantsStatment = _jdbcConnection.prepareStatement("SELECT v.pos, v.offset, v.word, v.sense, v.csco, v.status FROM eswn_variant v where v.pos = ? and v.offset=?");
	        _getVariantsTogetOffsets = _jdbcConnection.prepareStatement("SELECT v.pos, v.offset, v.word, v.sense, v.csco, v.status FROM eswn_variant v where v.pos = ? and v.word=?");
	        _getSynsetStatment =  _jdbcConnection.prepareStatement("SELECT pos, offset, sons, lexical, gloss FROM eswn_synset where pos = ? and offset=?");
			_getDirectRelationStatment = _jdbcConnection.prepareStatement("SELECT s.pos, s.offset, s.sons, s.lexical, s.gloss, r.csco from eswn_relation r, eswn_synset s  where  r.relation = ? and r.sourcePos = ? and r.sourceSynset = ? and r.targetPos = s.pos and r.targetSynset = s.offset");
			_getInverseRelationStatment = _jdbcConnection.prepareStatement("SELECT s.pos, s.offset, s.sons, s.lexical, s.gloss, r.csco from eswn_relation r, eswn_synset s  where  r.relation = ? and r.targetPos = ? and r.targetSynset = ? and r.sourcePos = s.pos and r.sourceSynset = s.offset");	
		} catch (InstantiationException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IllegalAccessException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	public static WordNetConnection getInstance()
	{
		if (_wnconnection == null)
		{
			_wnconnection = new WordNetConnection(); 		
		}
		return _wnconnection;
	}
	

	public PreparedStatement getVariantsPE()
	{
		return _getVariantsStatment;
	}
	
	public PreparedStatement getVariantsTogetOffsetsPE()
	{
		return _getVariantsTogetOffsets;
	}
	
	public PreparedStatement getSynsetPE()
	{
		return _getSynsetStatment;
	}
	
	public PreparedStatement getRelationDirectPE()
	{
		return _getDirectRelationStatment;
	}
	
	public PreparedStatement getRelationInversePE()
	{
		return _getInverseRelationStatment;
	}
	
	public void closeConnection()
	{
		if (_jdbcConnection != null)
		{
			try {
				_getSynsetStatment.close();
				_getVariantsStatment.close();
				_getVariantsTogetOffsets.close();
				_getDirectRelationStatment.close();
				_getInverseRelationStatment.close();
				_jdbcConnection.close();
				_jdbcConnection = null;
				_wnconnection = null;
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				_getSynsetStatment = null;
				_getVariantsStatment = null;
				_getVariantsTogetOffsets = null;
				_getDirectRelationStatment = null;
				_getInverseRelationStatment = null;
				_jdbcConnection = null;
				e.printStackTrace();
			}
		}
	}
	
	

	
}
	

